package com.newbsd.web.maventest.servlet;

import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.tomcat.util.http.fileupload.FileItem;
import org.apache.tomcat.util.http.fileupload.FileUploadException;
import org.apache.tomcat.util.http.fileupload.disk.DiskFileItemFactory;
import org.apache.tomcat.util.http.fileupload.servlet.ServletFileUpload;
import org.apache.tomcat.util.http.fileupload.servlet.ServletRequestContext;




import com.newbsd.web.maventest.dao.LoginDao;
import com.newbsd.web.maventest.pojo.User;
import com.newbsd.web.maventest.util.HttpUtil;


/**
 * @author 张成玉-registerServlet
 * Servlet implementation class RegisterServlet
 */
public class RegisterServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		doPost(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		request.setCharacterEncoding("utf-8");
		response.setCharacterEncoding("utf-8");
		LoginDao loginDao = new LoginDao();
		User user = new User();
		
		// 补全pojo
		user.setStatus("");
		user.setLoginip("");

		user.setImagepath("");

		// 配置上传的参数
		DiskFileItemFactory factory = new DiskFileItemFactory();
		// 设置缓存目录
		factory.setRepository(new File(System.getProperty("java.io.tmpdir")));
		
		ServletFileUpload upload = new ServletFileUpload(factory);
		upload.setHeaderEncoding("utf-8");

		// 上传路径设置
		String uploadPath = "E:\\web\\web2016\\web2016\\apache-tomcat-8.0.27-windows-x64\\apache-tomcat-8.0.27\\wtpwebapps\\zcy-12306"+"\\upload";
		// 创建上传目录
		File uploadDir = new File(uploadPath);		
		if (!uploadDir.exists()) {
			uploadDir.mkdir();// 创建路径
		}
		// 解析 request
		try {
			Map<String, String> map = new HashMap<>();
			List<FileItem> list = upload.parseRequest(new ServletRequestContext(request));// 可以同时处理多个文件
			if (list != null && list.size() > 0) {
				for (FileItem fileItem : list) {
					if(fileItem.isFormField()){
						map.put(fileItem.getFieldName(), fileItem.getString("UTF-8"));
					}else {
						String fileName = new File(fileItem.getName()).getName();// 从对象里获取名称
						String filePath = uploadPath + File.separator + fileName;
						File file = new File(filePath);// "/"
						fileItem.write(file);	
						user.setImagepath(filePath);
					}
				}
			}
			user.setUsername(map.get("username"));
			user.setPassword(map.get("password"));
			user.setRealname(map.get("realname"));
			user.setSex(map.get("sex"));
			user.setCertType(map.get("certType"));
			user.setProvince(map.get("province"));
			user.setCity(map.get("city"));
			user.setCert(map.get("cert"));
			user.setBirthday(map.get("birthday"));
			user.setUserType(map.get("userType"));
			user.setContent(map.get("content"));
			user.setRule("1");
		} catch (FileUploadException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		System.out.println(user);
		User result = null;
		try {
			result = loginDao.registUser(user);
		} catch (Exception e) {
			e.printStackTrace();
		}
		request.setAttribute("register", "success");
		HttpUtil.forward(request, response, "login.jsp");

	}

}
